package org.demo.controller;

import org.demo.common.Result;
import org.demo.entity.*;
import org.demo.mapper.NoticeMapper;
import org.demo.mapper.UsersMapper;
import org.demo.service.ICommunityService;
import org.demo.service.INoticeService;
import org.demo.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//通知表
@RestController
@RequestMapping("/notice")
public class NoticeConteoller {
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("/add")
    public Result add(@RequestBody Notice notice){
        if (!notice.getId().equals("0")){
            return Result.error("合同已支付");
        }else {
            Users users = usersMapper.selectById(notice.getAdminId());
            noticeService.addNotice(notice.getAdminId(),notice.getUserId(),notice.getHouseId(),users.getNickname()+notice.getContent(),"0","0","0");
            return Result.success();
        }
    }
    @PostMapping("/addadmin")
    public Result addadmin(@RequestBody Notice notice){
        noticeService.addadmin(notice);
        return Result.success();

    }
    @GetMapping("/Pagelist")
    public Result<PageBean<Notice>> Pagelist(@RequestParam Integer pageNum,
                                             @RequestParam Integer pageSize,
                                             @ModelAttribute Notice notice){
        PageBean<Notice> pb= noticeService.Pagelist(pageNum,pageSize,notice);
        return Result.success(pb);
    }
    @PostMapping("/list")
    public Result<List<Notice>> list(@RequestBody (required = false) Notice notice){
        List<Notice> list= noticeService.list(notice);
        return Result.success(list);
    }
    @PostMapping("/update")
    public Result update(@RequestBody Notice notice){
        noticeService.update(notice);
        return Result.success();
    }
    @DeleteMapping("/delete/{id}")
    public  Result delete(@PathVariable String id){
        Notice notice = noticeMapper.selectById(id);
        if (notice.getRolesCustomer().equals("1")||notice.getRolesIntermediary().equals("1")||notice.getRolesLandlord().equals("1")){
            Map<String,Object> map = ThreadLocalUtil.get();
            String ids = (String) map.get("id");
            if (ids.equals("admin")){
                noticeService.delete(id);
                return Result.success();
            }else {
                return Result.error("管理员通知仅管理员删除");
            }

        }else {
            noticeService.delete(id);
            return Result.success();
        }
    }

}
